package com.cskaoyan.service;

import com.cskaoyan.bean.MarketComment;
import com.cskaoyan.bean.MarketCommentExample;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.WxCommentCountVo;
import com.cskaoyan.bean.vo.WxCommentUserInfoVo;
import com.cskaoyan.bean.vo.WxCommentVo;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author yi
 * @date 2022/10/14 17:31
 */

@Service
public class WxCommentServiceImpl implements WxCommentService {
    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Autowired
    MarketUserMapper marketUserMapper;

    @Override
    public CommonData<WxCommentVo> queryComment(Integer valueId, Byte type, Integer showType, BasePageInfo basePageInfo) {

        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());

        // 根据专题Id找到评论
        MarketCommentExample marketCommentExample = getCommentExample(valueId, type, showType);
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);
        long total = marketCommentMapper.countByExample(marketCommentExample);

        // 转换成Vo
        List<WxCommentVo> wxCommentVos = new LinkedList<>();
        for (MarketComment marketComment : marketComments) {
            WxCommentVo wxCommentVo = CommentsToVo(marketComment);
            wxCommentVos.add(wxCommentVo);
        }

        // Vo封装进PageInfo
        PageInfo<WxCommentVo> wxCommentVoPageInfo = new PageInfo<>(wxCommentVos);
        CommonData<WxCommentVo> data = CommonData.data(wxCommentVoPageInfo);

        // sql查询的时候返回的是MarketComment，转换为vo之后才输入到page，结果会使得data中的total为limit，这里把它改回查询到的项的总数
        data.setTotal((int) total);
        return data;
    }

    private MarketCommentExample getCommentExample(Integer valueId, Byte type, Integer showType) {

        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();

        criteria.andValueIdEqualTo(valueId);
        criteria.andTypeEqualTo(type);
        criteria.andDeletedEqualTo(false);
        if (showType == 1) {
            criteria.andHasPictureEqualTo(true);
        }

        return marketCommentExample;
    }

    @Override
    public WxCommentCountVo queryCommentCount(Integer valueId, Byte type) {
        // 根据专题Id找到评论
        MarketCommentExample marketCommentExample = getCommentExample(valueId, type, 0);
        Long allCount = marketCommentMapper.countByExample(marketCommentExample);

        MarketCommentExample marketCommentExample2 = getCommentExample(valueId, type, 1);
        Long hasPicCount = marketCommentMapper.countByExample(marketCommentExample2);

        WxCommentCountVo wxCommentCountVo = new WxCommentCountVo();
        wxCommentCountVo.setAllCount(allCount);
        wxCommentCountVo.setHasPicCount(hasPicCount);
        return wxCommentCountVo;
    }

    @Override
    public MarketComment insertComment(MarketComment marketComment, Integer userId) {
        Date date = new Date();
        marketComment.setUserId(userId);
        marketComment.setAddTime(date);
        marketComment.setUpdateTime(date);
        marketCommentMapper.insertSelective(marketComment);
        return marketComment;
    }

    private WxCommentVo CommentsToVo(MarketComment marketComment) {
        // 根据评论的userId找到user信息
        MarketUser marketUser = marketUserMapper.selectByPrimaryKey(marketComment.getUserId());
        WxCommentUserInfoVo wxCommentUserInfoVo = new WxCommentUserInfoVo();
        wxCommentUserInfoVo.setNickName(marketUser.getNickname());
        wxCommentUserInfoVo.setAvatarUrl(marketUser.getAvatar());

        // 写入Vo
        WxCommentVo wxCommentVo = new WxCommentVo();
        wxCommentVo.setAddTime(marketComment.getAddTime());
        wxCommentVo.setPicList(marketComment.getPicUrls());
        wxCommentVo.setAdminContent(marketComment.getAdminContent());
        wxCommentVo.setContent(marketComment.getContent());
        wxCommentVo.setUserInfo(wxCommentUserInfoVo);

        return wxCommentVo;
    }
}
